package edu.uba.fcen.estimacion.estimacion.graphics;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.uba.fcen.estimacion.word.selection.Pair;
import edu.uba.fcen.estimacion.word.selection.filter.FilterWords;
import edu.uba.fcen.estimacion.word.selection.filter.FilterWordsFactory;

public class GoldStandardVsSystem {

	public static void main(String[] args) {
		String pathToGoldStandardCSV = args[0];
		
		Map<String, List<Double>> means = calculateMeans(pathToGoldStandardCSV);
		List<Double> mean;
		StringBuilder forR;

		
		StringBuilder system = new StringBuilder();
		StringBuilder gold = new StringBuilder();
		Map<Double, Double> map = new HashMap<Double, Double>();
		List<Double> gsAgrado = means.get("agradabilidadGoldStandar");
		List<Pair<Double, Double>> pairs = new ArrayList<Pair<Double,Double>>();
		int indexx = 0;
		for(Double value : means.get("agradabilidadSistema")) {
			pairs.add(new Pair<Double, Double>(value, gsAgrado.get(indexx)));
			indexx++;
		}
		Collections.sort(pairs, new Comparator<Pair<Double,Double>>() {

			@Override
			public int compare(Pair<Double,Double> o1, Pair<Double,Double> o2) {
				return o2.getY().compareTo(o1.getY());
			}
		});
		system.append("agradabilidadSistema = c(");
		gold.append("agradabilidadGoldStandar = c(");
		for (Pair<Double, Double> pair : pairs) {
			system.append(pair.getX() + ",");
			gold.append(pair.getY() + ",");
		}
		System.out.println(gold.toString());
		System.out.println(system.toString());
		
		pairs.clear();
		system = new StringBuilder();
		gold = new StringBuilder();
		
		List<Double> gsActivacion = means.get("activacionGoldStandar");
		indexx = 0;
		for(Double value : means.get("activacionSistema")) {
			pairs.add(new Pair<Double, Double>(value, gsActivacion.get(indexx)));
			indexx++;
		}
		Collections.sort(pairs, new Comparator<Pair<Double,Double>>() {

			@Override
			public int compare(Pair<Double,Double> o1, Pair<Double,Double> o2) {
				return o2.getY().compareTo(o1.getY());
			}
		});
		system.append("activacionSistema = c(");
		gold.append("activacionGoldStandar = c(");
		for (Pair<Double, Double> pair : pairs) {
			system.append(pair.getX() + ",");
			gold.append(pair.getY() + ",");
		}
		System.out.println(gold.toString());
		System.out.println(system.toString());

		pairs.clear();
		system = new StringBuilder();
		gold = new StringBuilder();
				
		List<Double> gsImg = means.get("imaginabilidadGoldStandar");
		indexx = 0;
		for(Double value : means.get("imaginabilidadSistema")) {
			pairs.add(new Pair<Double, Double>(value, gsActivacion.get(indexx)));
			indexx++;
		}
		Collections.sort(pairs, new Comparator<Pair<Double,Double>>() {

			@Override
			public int compare(Pair<Double,Double> o1, Pair<Double,Double> o2) {
				return o2.getY().compareTo(o1.getY());
			}
		});
		system.append("imaginabilidadSistema = c(");
		gold.append("imaginabilidadGoldStandar = c(");
		for (Pair<Double, Double> pair : pairs) {
			system.append(pair.getX() + ",");
			gold.append(pair.getY() + ",");
		}
		System.out.println(gold.toString());
		System.out.println(system.toString());
		
		
//		for (String key : means.keySet()) {
//			forR = new StringBuilder();
//			forR.append(key + " = c(");
//			mean = means.get(key);
//			int index = 0;
//			for (Double value : mean) {
//				if (index == mean.size()-1) {
//					forR.append(value.toString() + ")");
//				} else {
//					forR.append(value.toString() + ",");
//				}
//				index++;
//			}
//			System.out.println(forR.toString());
//		}
	}

	private static Map<String, List<Double>> calculateMeans(String csvFile) {
		String line; 
		String [] splitLine;
		List<Double> agradabilidadSist = new ArrayList<Double>();
		List<Double> activacionSist = new ArrayList<Double>();
		List<Double> imaginabilidadSist = new ArrayList<Double>();
		
		List<Double> agradabilidadGs = new ArrayList<Double>();
		List<Double> activacionGs = new ArrayList<Double>();
		List<Double> imaginabilidadGs = new ArrayList<Double>();
		
		double meanAgradoGs = 0d;
		double meanActGs = 0d;
		double meanImagGs = 0d;

		final int agradabilidad1 = 5;
		final int agradabilidad2 = 8;
		final int agradabilidad3 = 11;
		final int agradabilidad4 = 14;
		final int agradabilidad5 = 17;
		
		final int activacion1 = 6;
		final int activacion2 = 9;
		final int activacion3 = 12;
		final int activacion4 = 15;
		final int activacion5 = 16;
		
		final int imaginabilidad1 = 6;
		final int imaginabilidad2 = 9;
		final int imaginabilidad3 = 12;
		final int imaginabilidad4 = 15;
		final int imaginabilidad5 = 16;
		
		FilterWords fw = FilterWordsFactory.getNormalLineByLineFilter(csvFile);
		fw.readLine();
		while ((line=fw.readLine())!=null) {
			splitLine = line.split(",");
			meanAgradoGs = (Double.valueOf(splitLine[agradabilidad1]) + Double.valueOf(splitLine[agradabilidad2]) +
					Double.valueOf(splitLine[agradabilidad3]) + Double.valueOf(splitLine[agradabilidad4]) +
					Double.valueOf(splitLine[agradabilidad5]))/5;
			
			meanActGs = (Double.valueOf(splitLine[activacion1]) + Double.valueOf(splitLine[activacion2]) +
					Double.valueOf(splitLine[activacion3]) + Double.valueOf(splitLine[activacion4]) + 
					Double.valueOf(splitLine[activacion5]))/5;
			
			meanImagGs = (Double.valueOf(splitLine[imaginabilidad1]) + Double.valueOf(splitLine[imaginabilidad2]) +
					Double.valueOf(splitLine[imaginabilidad3]) + Double.valueOf(splitLine[imaginabilidad4]) + 
					Double.valueOf(splitLine[imaginabilidad5]))/5;
			
			agradabilidadGs.add(meanAgradoGs);
			activacionGs.add(meanActGs);
			imaginabilidadGs.add(meanImagGs);
			
			agradabilidadSist.add(Double.valueOf(splitLine[2]));
			activacionSist.add(Double.valueOf(splitLine[3]));
			imaginabilidadSist.add(Double.valueOf(splitLine[4]));
			
		}
		
		Map<String, List<Double>> ret = new HashMap<String, List<Double>>();
		ret.put("agradabilidadSistema", agradabilidadSist);
		ret.put("activacionSistema", activacionSist);
		ret.put("imaginabilidadSistema", imaginabilidadSist);
		
		ret.put("agradabilidadGoldStandar", agradabilidadGs);
		ret.put("activacionGoldStandar", activacionGs);
		ret.put("imaginabilidadGoldStandar", imaginabilidadGs);
		return ret;
	}
	
	private static List<Pair<Double, Double>> getSortedList(Map<Double, Double> map) {
		List<Pair<Double, Double>> sortedList = new ArrayList<Pair<Double,Double>>(map.size());
		
		for (Double key : map.keySet()) {
			sortedList.add(new Pair<Double, Double>(key, map.get(key)));
		}
		Collections.sort(sortedList, new Comparator<Pair<Double,Double>>() {

			@Override
			public int compare(Pair<Double,Double> o1, Pair<Double,Double> o2) {
				return o2.getY().compareTo(o1.getY());
			}
		});
		return sortedList;
	}
	
}
